package com.nagat.http;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;

public class Client {

	
	public static void main(String[] args) throws Exception{
		
		
		HttpClient client = new DefaultHttpClient();
		
		HttpPost post = new HttpPost();

		post.setURI(new URI("http://localhost:8080/login"));
		
		List <NameValuePair> nvps = new ArrayList <NameValuePair>();
        nvps.add(new BasicNameValuePair("username", "admin"));
        nvps.add(new BasicNameValuePair("password", "admin"));

        post.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
		
        post.setHeader("username", "hadmin");
        post.setHeader("password", "hadmin");
        
        post.getParams().setParameter("username", "admin");
        post.getParams().setParameter("password", "admin");
        
        ResponseHandler<String> response = new BasicResponseHandler();
        
        String ret = client.execute(post, response);
        System.out.println(ret);
        
        
//		HttpResponse response = client.execute(post);
//		
//		System.out.println(response.getStatusLine());
//		
//		HttpEntity httpEntity = response.getEntity();
//		
//		InputStream is = httpEntity.getContent();
//		
//		BufferedReader in =new BufferedReader(new InputStreamReader(is));
//
//		StringBuffer buffer =new StringBuffer();
//
//		String line = "";
//
//		while((line = in.readLine()) != null){
//
//			buffer.append(line);
//			
//		}
		
		post.abort();
		
		client.getConnectionManager().shutdown();
		
//		System.out.println(buffer.toString());
		
	}
	
	
}
